<template>
  <el-dialog :visible.sync="visible" width="80%" custom-class="default-dialog" :close-on-click-modal="false"
    @close="cancel" append-to-body v-dialogDrags>
    <div slot="title">
      <img v-if="type" :src="require(`@/assets/img/${type}.png`)" alt="">
      <span>{{ title }} </span>
    </div>
    <el-form :model="form" :rules="rules" ref="form" label-width="120px" class="dialog-form" :disabled="type == 'view'">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="河湖名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入河湖名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="河湖类型" prop="typeName">
            <el-select size="small" v-model="form.type"  placeholder="请选择">
              <el-option
                v-for="item in hhlxOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
<!--            <el-cascader v-model="form.type" :options="hehuOptions" placeholder="请选择" clearable></el-cascader>-->
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="河湖介绍" prop="info">
            <el-input v-model="form.info" placeholder="请输入河湖介绍"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="复苏河长" prop="recoveryLength">
            <el-input type="number" v-model="form.recoveryLength" placeholder="请输入复苏河长"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="湖泊面积" prop="area">
            <el-input v-model="form.area" placeholder="请输入湖泊面积" type="number"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="起点" prop="origin">
            <el-input v-model="form.origin" placeholder="请输入起点"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="终点" prop="estuary">
            <el-input v-model="form.estuary" placeholder="请输入终点"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="管理主体" prop="manage">
            <el-input v-model="form.manage" placeholder="请输入管理主体"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="责任主体" prop="responsiblePartyId">
<!--            <el-input v-model="form.responsiblePartyName" placeholder="请输入责任主体"></el-input>-->
            <el-select size="small" v-model="form.responsiblePartyId"  placeholder="请选择">
              <el-option
                v-for="item in zrztOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="补水目标值" prop="target">
            <el-input v-model="form.target" placeholder="请输入补水目标值" type="number"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="目标信息" prop="targetInfo">
            <el-input v-model="form.targetInfo" placeholder="请输入责任主体"></el-input>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <span slot="footer">
      <el-button size="small" @click="cancel">取 消</el-button>
      <el-button size="small" v-if="type != 'view'" type="primary" @click="sure">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      title: '',
      form: {
      },
      hehuOptions:[],
      rules: {
        name: [{ required: true, message: '请输入河湖名称', trigger: 'blur' }],
        estuary: [{ required: true, message: '请输入终点', trigger: 'blur' }],
        manage:[{ required: true, message: '请输入管理主体', trigger: 'blur' }],
        origin:[{ required: true, message: '请输入起点', trigger: 'blur' }],
        type:[{ required: true, message: '请输入河湖类型', trigger: 'blur' }],
        responsiblePartyId:[{ required: true, message: '请输入责任主体', trigger: 'blur' }],
      },
      type: '',
      zrztOptions: [],
      hhlxOptions: [
        {
          value: 1,
          label: '河流'
        }, {
          value: 2,
          label: '湖泊'
        }
      ],
    };
  },
  methods: {
    async showModal(type, row) {
      this.visible = true
      this.type = type
      await this.getParty()
      if (type == 'add') {
        this.title = '新增'
        this.form = {
        }
      } else if (type == 'edit') {
        this.title = '编辑'
        this.getInfoInit(row.id)
      } else if (type == 'view') {
        this.title = '详情'
        this.getInfoInit(row.id)
      }
    },
   async getParty(){
     let res = await this.$api.getParty();
     if (res && res.code === 200) {
       console.log(res.data)
       let data = res.data || {}
       this.zrztOptions = data
     }
    },
    // 详情
    getInfoInit(id) {
      this.$api.getHeHuInfo({ id: id }).then(res => {
        if (res && res.code == 200) {
          let data = res.data || {}
          data.name = data.riverName
          if (data.type=== 1) {
            data.typeName='河流'
          }else if (data.type=== 2) {
            data.typeName='湖泊'
          }else if (data.type=== 3) {
            data.typeName='管理'
          }
          this.form = data
        }
      })
    },
    // 确定
    sure() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.type == 'add') {
            this.$api.addHeHuInfo(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          } else if (this.type == 'edit') {
            this.$api.updateHeHuInfo(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          }
        }
      });
    },
    // 取消
    cancel() {
      this.visible = false;
      this.$refs["form"].resetFields();
    },
  }
};
</script>
<style lang="less" scoped>
.el-form-item__label {
  width: 150px;
}
</style>
